package com.md.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.AliOcrUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.CarFirstAid;
import com.md.api.entity.UserCar;
import com.md.api.entity.HttpResult;
import com.md.api.entity.UserCar;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.CarService;
import com.md.api.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/car")
@RestController
public class CarController {


	@Resource
	CarService carService;

	//我的优惠券
	@RequestMapping("/myCar")
	@ResponseBody
	public HttpResult myCar(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return carService.myCar(request,qto);
	}

	@NoRepeatSubmit
	@RequestMapping("/ocr")
	public HttpResult ocr(@RequestBody UserCar userCar) {
		if (StringUtils.isEmpty(userCar.getDrivingLicenseImg())){
			return HttpResult.PARAM_ERROR;
		}
		JSONObject identify = AliOcrUtil.identify(userCar.getDrivingLicenseImg());
		if (null==identify){
			return HttpResult.fail("请正确上传行驶证正面照片");
		}

		return new HttpResult(identify);
	}
	@NoRepeatSubmit
	@RequestMapping("/addCar")
	public HttpResult addCar(HttpServletRequest request,@RequestBody UserCar userCar) {
		return carService.addCar(request,userCar);
	}


	@NoRepeatSubmit
	@RequestMapping("/deleteCar")
	public HttpResult deleteCar(@RequestBody UserCar userCar) {
		if (null==userCar.getCarId()){
			return HttpResult.PARAM_ERROR;
		}
		return carService.deleteCar(userCar);
	}

	@NoRepeatSubmit
	@RequestMapping("/setDefault")
	public HttpResult setDefault(HttpServletRequest request,@RequestBody UserCar userCar) {
		if (null==userCar.getCarId()){
			return HttpResult.PARAM_ERROR;
		}
		return carService.setDefault(request,userCar);
	}


	@NoRepeatSubmit
	@RequestMapping("/updateCar")
	public HttpResult updateCar(@RequestBody UserCar userCar) {
		if (null==userCar.getCarId()){
			return HttpResult.PARAM_ERROR;
		}
		return carService.updateCar(userCar);
	}


	@RequestMapping("/typeList")
	public HttpResult typeList() {
		return carService.typeList();
	}



	@NoRepeatSubmit
	@RequestMapping("/firstAid")
	public HttpResult firstAid(HttpServletRequest request,@RequestBody CarFirstAid firstAid) {
		return carService.firstAid(request,firstAid);
	}


	@NoRepeatSubmit
	@RequestMapping("/logoPage")
	public HttpResult logoPage(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {
		return carService.logoPage(request,qto);
	}

}
